import axios from 'axios'

const http = axios.create({
    timeout: 120000
})

// 添加请求拦截器
http.interceptors.request.use(config => {
    const token = sessionStorage.getItem('token')
    console.log(`http.request => ${config.method} ${config.url}`)
    if (typeof (token) !== 'undefined') {
        config.headers.Authorization = token
        console.log('http.request => set token success')
    }
    return config
}, error => {
    console.log(`http.request => ${error.config.method}:${error.config.url} ${error}`)
    return Promise.reject(error)
})

// 添加响应拦截器
http.interceptors.response.use(response => {
    // 2xx 范围内的状态码都会触发该函数。
    // 对响应数据做点什么
    console.log(`http.response => ${response.config.method}:${response.config.url} ${response.status} ${response.statusText}`)
    return response
}, error => {
    // 超出 2xx 范围的状态码都会触发该函数。
    // 对响应错误做点什么
    console.log(`http.response => ${error.response.config.method}:${error.response.config.url} ${error.response.status} ${error.response.statusText} ${error.message}`)
    return Promise.reject(error)
})

export default http